package com.xmall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmall.member.utils.ConvertUtils;
import com.xmall.define.dto.member.DeliveryAddressDto;
import com.xmall.member.entity.DeliveryAddress;
import com.xmall.member.mapper.DeliveryAddressMapper;
import com.xmall.member.define.request.DeliveryAddressUpdateRequest;
import com.xmall.member.service.IDeliveryAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmall.utils.XmallUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 收货地址 服务实现类
 */
@Service
public class DeliveryAddressServiceImpl extends ServiceImpl<DeliveryAddressMapper, DeliveryAddress> implements IDeliveryAddressService {

    /**
     * 详情
     */
    @Override
    public DeliveryAddressDto info(Long id) {
        DeliveryAddress entity = this.getById(id);
        XmallUtils.notNull(entity);
        return ConvertUtils.AddressModule.INSTANCE.toDto(entity);
    }

    /**
     * 列表
     */
    @Override
    public List<DeliveryAddressDto> list(Long memberId) {
        List<DeliveryAddress> list = this.list(new QueryWrapper<DeliveryAddress>().eq("member_id", memberId));
        return ConvertUtils.AddressModule.INSTANCE.toDtoList(list);
    }

    /**
     * 保存
     */
    @Transactional
    @Override
    public void saveEntity(DeliveryAddressUpdateRequest request) {
        // TODO 默认地址
        DeliveryAddress updateEntity = new DeliveryAddress();
        BeanUtils.copyProperties(request, updateEntity);
        this.save(updateEntity);
    }

    /**
     * 编辑
     */
    @Transactional
    @Override
    public void updateEntity(DeliveryAddressUpdateRequest request) {
        // TODO 默认地址
        DeliveryAddress updateEntity = new DeliveryAddress();
        BeanUtils.copyProperties(request, updateEntity);
        this.updateById(updateEntity);
    }

    /**
     * 删除
     */
    @Override
    public void deleteEntity(Long id) {
        DeliveryAddress entity = this.getById(id);
        XmallUtils.notNull(entity);
        this.removeById(id);
    }
}
